package ink.xiaobaibai.service.monthly.impl;

import ink.xiaobaibai.common.delay.DelayItemByCoupon;
import ink.xiaobaibai.common.delay.DeviceDelayItem;
import ink.xiaobaibai.common.delay.MyDelayQueue;
import ink.xiaobaibai.service.ICouponCodeLogService;
import ink.xiaobaibai.service.ICouponCodeService;
import ink.xiaobaibai.service.ICouponService;
import ink.xiaobaibai.service.ICouponUserService;
import ink.xiaobaibai.service.monthly.IDelayTaskCommonService;
import ink.xiaobaibai.serviceOxy.pay.IPayHttpService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @description: 优惠券锁定状态的延时队列
 * @author: 小白白
 * @create: 2021-06-06
 **/

@Service
public class DelayTaskCommonServiceImpl implements IDelayTaskCommonService, ApplicationRunner {

    @Autowired
    private IPayHttpService payHttpService;

    @Autowired
    private ICouponUserService couponUserService;

    @Autowired
    private ICouponService couponService;

    @Autowired
    private ICouponCodeService couponCodeService;

    @Autowired
    private ICouponCodeLogService couponCodeLogService;

    // 已经ShutdownProtectionService保护
    public MyDelayQueue<DeviceDelayItem> delayQueue = new MyDelayQueue<>();

    @Override
    public void couponAddCheckStatus(Integer couponId, Integer couponUserId, String orderId) {
        this.delayQueue.add(new DelayItemByCoupon(couponId, couponUserId, orderId, DateUtils.addSeconds(new Date(), 70)));
    }

    @Override
    public void couponCodeAddCheckStatus(String code, String orderId) {
        DelayItemByCoupon delayItemByCoupon = new DelayItemByCoupon(null, null, orderId, DateUtils.addSeconds(new Date(), 70));
        delayItemByCoupon.setCouponCode(code);
        this.delayQueue.add(delayItemByCoupon);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        ExecutorService executorService= Executors.newSingleThreadExecutor();
//        executorService.execute(()->{
//            while (true){
//                try {
//                    DelayItemByCoupon item =(DelayItemByCoupon) delayQueue.take();
//                    /**
//                     * 使用http去查询对应的订单是否已经支付了
//                     */
//                    UpdateWrapper<CouponUser> u1=new UpdateWrapper<>();
//                    u1.eq("id",item.getCouponUserId());
//                    if(payHttpService.queryOrderHavePaid(item.getOrderId())){
//                        if (item.getCouponId() != null) {
//                            //支付成功,改为已用,并且优惠券已用数量+1
//                            //还是去除优惠券的锁定状态吧
//                            u1.set("status", CouponStatus.USED.getStatus());
//                            Integer couponId = item.getCouponId();
//                            UpdateWrapper<Coupon> u2 = new UpdateWrapper<>();
//                            u2.eq("id", couponId);
//                            u2.setSql("use_count=use_count+1");
//                            couponService.update(u2);
//                            couponUserService.update(u1);
//                        } else{
//                            // 优惠码使用记录
//                            CouponCodeLog couponCodeLog = new CouponCodeLog(item.getCouponCode(),item.getOrderId());
//                            couponCodeLogService.save(couponCodeLog);
//                            // 优惠码变成已经使用
//                            couponCodeService.useCouponCode(item.getCouponCode());
//                        }
//                    }else{
//                        if (item.getCouponId() != null) {
//                            //未支付
////                            u1.set("status", CouponStatus.CAN_USE.getStatus());
////                            couponUserService.update(u1);
//                        }else{
//                            // 本来就没有变成可用状态,就不用解锁
//                            //couponCodeService.unlockCouponCode(item.getCouponCode());
//                        }
//                    }
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }

}
